import 'dart:io';

import 'package:flutter/services.dart';
import 'package:dio/dio.dart';
import '../s_utils.dart';

class AppAttribution {
  static late Future Function(Map<String, dynamic> params) fetchAttribution;

  static load({String? slug}) async {
    Map attribution = {};
    // 从剪贴板中获取slug
    // String slug = await getSlugFromClipboard();

    DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
    Map<String, dynamic> params = {
      "slug": slug,
      "platform": Platform.operatingSystem,
      "screen_width": Sizes.screenWidth.toInt(),
      "screen_height": Sizes.screenHeight.toInt(),
      "app_install_id": null,
    };

    if (Platform.isIOS) {
      IosDeviceInfo info = await deviceInfo.iosInfo;
      params['app_install_id'] = info.identifierForVendor;
    }

    logger.w("初始化Attribution: 参数$params");

    try {
      Response res = await fetchAttribution(params);
      attribution = res.data;
      SharedPrefs.putString('attribution_slug', attribution['slug']);
    } catch (e) {}
  }

  static getSlugFromClipboard() async {
    ClipboardData? data = await Clipboard.getData(Clipboard.kTextPlain);
    return data?.text ?? '';
  }
}
